
// pages/shop/shop.js

// pages/my/my.js
let that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
        ic: 'huiyuan',
        tit: '会员中心'
      },
      {
        ic: 'shoucang',
        tit: '我的收藏'
      },
      {
        ic: 'zuoye',
        tit: '我的作业'
      },
      {
        ic: 'tiwen',
        tit: '我的提问'
      },
      {
        ic: 'duihuanma',
        tit: '兑换码'
      },
      {
        ic: 'icon-',
        tit: '收货地址'
      },
      {
        ic: 'zuoye',
        tit: '购物车'
      },
      {
        ic: 'xiaoxi',
        tit: '我的消息'
      },
      {
        ic: 'kefu',
        tit: '在线客服'
      },
      {
        ic: 'tiwen',
        tit: '意见反馈'
      },
      {
        ic: 'zuoye',
        tit: '版本更新'
      },
      {
        ic: 'zuoye',
        tit: '退出登录'
      }
    ],
    userInfo: wx.getStorageSync('userInfo') || {},
    cloudID: '',
    encryptedData: '',
    iv: '',
    token: ''
  },
  //点击列表事件
  ad(e){
    console.log(e);
    var index=e.currentTarget.dataset.index;
    if(index==11){
      //调用退出登录事件
      that.clear_token();
    }
  },
  //退出登录
  clear_token(){
    that.data.token='';
    wx.setStorageSync('token', that.data.token);
    that.onLoad();
  },
  //授权登录
  userInfo(e) {
    console.log(e);
    wx.getSetting({
      success: res => {
        console.log(res);
        //如果用户授权了，就会获取信息
        if (res.authSetting['scope.userInfo']) {
          that.setData({
            userInfo: e.detail.userInfo,
            iv: e.detail.iv,
            encryptedData: e.detail.encryptedData,
            cloudID: e.detail.cloudID
          })
          wx.setStorageSync('userInfo', e.detail.userInfo);
          that.login();
        }else{//如果没有授权，就去设置页面设置授权
          that.showSeting();
        }
      },
      fail: msg => {
        console.log(msg)
      }
    })
  },
  //登录接口
  login() {
    wx.showLoading({
      title: '加载中...',
    });
    wx.login({
      success(res) {
        console.log(res);
        wx.request({
          url: 'https://api.it120.cc/yanger/user/wxapp/login',
          data: {
            code: res.code
          },
          success(res1) {
            console.log(res1);
            if (res1.data.code == 10000) {
              this.zhuCe();
              return false;
            } else if (res1.data.code == 0) {
              wx.setStorageSync('uid', res1.data.data.uid);
              wx.setStorageSync('openid', res1.data.data.openid);
              wx.setStorageSync('token', res1.data.data.token);
              wx.hideLoading();
              wx.showToast({
                title: '登录成功',
                icon: "success"
              })
              that.onLoad(); //再次调用一下，之后会重新回去数据
            }
          }
        })
      }
    })
  },
  //注册接口
  zhuCe() {
    wx.login({
      success(res) {
        wx.request({
          url: 'https://api.it120.cc/yanger/user/wxapp/register/complex',
          data: {
            iv: that.data.iv,
            encryptedData: that.data.encryptedData,
            code: res.code
          },
          success(res1) {
            console.log(res1);
            console.log("注册成功");
            this.login(); //在调用登录接口，登录页面
          }
        })
      }
    })
  },
  //设置授权页面
  showSeting(){
    wx.showModal({
      title: '提示！',
      confirmText: '去设置',
      showCancel: false,
      content: '请授权登录',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../login/login',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */


  onLoad: function(options) {
    that = this;
    that.setData({
      token: wx.getStorageSync('token')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})